﻿using System;
using Microsoft.Phone.Shell;

namespace Microsoft.Phone.Controls
{
    /// <summary>
    /// Implementation of <see cref="ApplicationBarIconButtonHost{T}">ApplicationBarIconButtonHost&lt;T&gt;</see>.
    /// </summary>
    public class ApplicationBarIconButtonHost : ApplicationBarIconButtonHost<ApplicationBarIconButton>
    {

    }

    /// <summary>
    /// Bindable alternate generic implementation of <see cref="IApplicationBarIconButton">IApplicationBarIconButton</see>.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApplicationBarIconButtonHost<T> : ApplicationBarItemBase, IApplicationBarIconButton
        where T : ApplicationBarIconButton, new()
    {
        private readonly T _button;

        /// <summary>
        /// Gets <see cref="ApplicationBarIconButton">button</see>.
        /// </summary>
        public T Button
        {
            get { return _button; }
        }

        /// <summary>
        /// Initializes new instance of class.
        /// </summary>
        public ApplicationBarIconButtonHost()
        {
            _button = new T {Text = "?"};
            _button.Click += OnClick;
        }

        /// <summary>
        /// The URI of the icon to use for the button.
        /// </summary>
        /// <returns>
        /// Type: <see cref="T:System.Uri"/>.
        /// </returns>
        public Uri IconUri
        {
            get { return Button.IconUri; }
            set { Button.IconUri = value; }
        }

        #region Overrides of ApplicationBarMenuItemHostBase

        /// <summary>
        /// Processes Text property changes.
        /// </summary>
        /// <param name="text"></param>
        protected override void OnTextChanged(string text)
        {
            Button.Text = text;
        }

        /// <summary>
        /// Processes IsEnabled property changes.
        /// </summary>
        /// <param name="isEnabled"></param>
        protected override void OnIsEnabledChanged(bool isEnabled)
        {
            Button.IsEnabled = isEnabled;
        }

        #endregion
    }
}